package com.wanxing.sellerapp.emqx

import android.util.Log
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.wanxing.sellerapp.App
import com.wanxing.sellerapp.config.EmqxConfig
import com.wanxing.sellerapp.emqx.entity.EmqMessage
import com.wanxing.sellerapp.emqx.enums.ActionEnum
import com.wanxing.sellerapp.seller.Door
import com.wanxing.sellerapp.seller.ISellerEventCallback
import com.wanxing.sellerapp.seller.SellerManager
import com.wanxing.sellerapp.seller.entity.ScaleWeight

class SellerEventCallback(private val mqtt: MyMqttClient,  private val seller: SellerManager)
    : ISellerEventCallback {


    companion object{
        const val TAG = "SellerEventCallback"
    }

    override fun onDoorClose(doorNo: Int, weights: MutableList<ScaleWeight>?) {
        Log.i(TAG, "#####onDoorClose#### doorNo: $doorNo, ${JSON.toJSONString(weights)} ")
        val data = JSONObject()
        data["weights"] = weights
        val message = EmqMessage()
        message.doorNo = doorNo
        message.mid = App.mid
        message.timestamp = System.currentTimeMillis()
        message.action = ActionEnum.CLOSE_DOOR
        message.data = data
        mqtt.publish(EmqxConfig.pushServiceTopic, JSON.toJSONString(message),mustArrive = true, qos = 2)
    }

    override fun onWeightChange(doorNo: Int, weights: MutableList<ScaleWeight>) {
        val data = JSONObject()
        data["weights"] = weights
        val message = EmqMessage()
        message.doorNo = doorNo
        message.mid = App.mid
        message.timestamp = System.currentTimeMillis()
        message.action = ActionEnum.WEIGHT_CHANGE
        message.data = data
        mqtt.publish(EmqxConfig.pushServiceTopic, JSON.toJSONString(message), qos = 0)
    }

    override fun onDoorWeightStable(doorNo: Int, weights: MutableList<ScaleWeight>?) {
        Log.i(TAG, "#####onDoorWeightStable#### doorNo: $doorNo, ${JSON.toJSONString(weights)}")
        Log.i(TAG, "#####onDoorClose#### doorNo: $doorNo, ${JSON.toJSONString(weights)} ")
//        val payload = JSONObject()
//        payload["type"] = Action.TYPE_CLOSE_DOOR_STABLE
//        payload["timestamp"] = System.currentTimeMillis()
//        val data = JSONObject()
//        data["doorNo"] = doorNo
//        data["weight"] = weights
//        payload["data"] = data
//        mqtt.publish("seller/${mqtt.uid}/push", payload.toJSONString(),mustArrive = true, qos = 0)
    }

//    override fun onWeightChange(doorNo: Int, weights: MutableList<ScaleWeight>?) {
//        val data = JSONObject()
//        data["weights"] = weights
//        val message = EmqMessage()
//        message.doorNo = doorNo
//        message.mid = App.mid
//        message.timestamp = System.currentTimeMillis()
//        message.action = ActionEnum.WEIGHT_CHANGE
//        message.data = data
//        mqtt.publish(EmqxConfig.pushServiceTopic, JSON.toJSONString(message),qos = 0)
//    }

    override fun onWeightLog(doors: MutableList<Door>?) {
        Log.i(TAG, "#####onWeightLog####")
    }
}